#!/bin/bash

# Function to check if a command exists
command_exists() {
    type "$1" &> /dev/null
}

# 1. 配置yum源为阿里云源
echo "开始配置yum源为阿里云源..."
cd /etc/yum.repos.d/ || { echo "无法进入目录 /etc/yum.repos.d/"; exit 1; }
if [ ! -f "/etc/yum.repos.d/CentOS-Base.repo" ]; then
    mkdir -p backup
    mv CentOS-Base.repo backup/ 2>/dev/null
    wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo || { echo "下载阿里云源失败"; exit 1; }
    yum clean all && yum makecache || { echo "清理yum缓存或建立缓存失败"; exit 1; }
else
    echo "阿里云源已存在，跳过配置步骤。"
fi
echo "yum源配置完成"

# 2. 安装docker
echo "开始安装docker..."
if ! command_exists docker; then
    yum install -y yum-utils device-mapper-persistent-data lvm2 || { echo "安装依赖失败"; exit 1; }
    yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo || { echo "添加Docker源失败"; exit 1; }
    yum install -y docker-ce || { echo "安装Docker失败"; exit 1; }
    systemctl start docker && systemctl enable docker || { echo "启动或设置Docker自启失败"; exit 1; }
else
    echo "Docker已安装，跳过安装步骤。"
fi
echo "docker安装完成"

# 3. 安装docker-compose
echo "开始安装docker-compose..." | tee -a "$LOGFILE"
DOCKER_COMPOSE_INSTALL_PATH="/usr/local/bin/"
if ! command -v docker-compose &> /dev/null; then
    curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o "$DOCKER_COMPOSE_INSTALL_PATH" || { echo "下载docker-compose失败" | tee -a "$LOGFILE"; exit 1; }
    chmod +x "$DOCKER_COMPOSE_INSTALL_PATH" || { echo "设置docker-compose执行权限失败" | tee -a "$LOGFILE"; exit 1; }
else
    echo "Docker Compose已安装，跳过安装步骤。" | tee -a "$LOGFILE"
fi
echo "docker-compose安装完成" | tee -a "$LOGFILE"

# 4. 配置docker
echo "开始配置docker..." | tee -a "$LOGFILE"
if [ -f "/etc/docker/daemon.json" ]; then
    echo "Docker配置文件已存在，将替换内容。" | tee -a "$LOGFILE"
    cat > /etc/docker/daemon.json <<EOF
{
  "registry-mirrors": [
        "https://docker.chenby.cn/",
        "https://0c105db5188026850f80c001def654a0.mirror.swr.myhuaweicloud.com",
        "https://dhub.kubesre.xyz/"
  ]
}
EOF
    systemctl daemon-reload && systemctl restart docker || { echo "重载系统服务或重启Docker失败" | tee -a "$LOGFILE"; exit 1; }
else
    mkdir -p /etc/docker
    cat > /etc/docker/daemon.json <<EOF
{
  "registry-mirrors": [
        "https://docker.chenby.cn/",
        "https://0c105db5188026850f80c001def654a0.mirror.swr.myhuaweicloud.com",
        "https://dhub.kubesre.xyz/"
  ]
}
EOF
    systemctl daemon-reload && systemctl restart docker || { echo "重载系统服务或重启Docker失败" | tee -a "$LOGFILE"; exit 1; }
fi
echo "docker配置完成" | tee -a "$LOGFILE"

# 5. docker-compose.yml文件生成并放在/app目录
echo "生成docker-compose.yml文件并放在/app目录..."
if [ ! -f "/app/docker-compose.yml" ]; then
    mkdir -p /app
    cat > /app/docker-compose.yml <<EOF
# 学校服务器 配置
version: '3'
services: 
  node:
    container_name: node
    image: 17729603616/nodeos:1.0.0
    restart: always
    tty: true
    command: bash
    ports:
      - 3001:3001
    working_dir: /app
    networks:
      nn:
        ipv4_address: 192.168.5.106
    volumes: 
      - /app/node:/app
      - /etc/localtime:/etc/localtime

  java:
    container_name: java
    image: guyang201/javakols:g1
    working_dir: /app
    restart: always
    tty: true
    ports:
      - 8080:8080
    networks:
      nn:
        ipv4_address: 192.168.5.104
    volumes: 
      - /app/java:/app
      - /etc/localtime:/etc/localtime
    command: 
      - bash

  redis:
    container_name: redis
    image: redis:5.0
    ports:
      - 6379:6379
    command: redis-server
    command: [
      '--appendonly yes',
      '--requirepass "root"'
    ]
    tty: true
    restart: always
    networks:
      nn:
        ipv4_address: 192.168.5.103
    volumes: 
      - /app/redis:/app
      - /etc/localtime:/etc/localtime
  
  mysql:
    container_name: mysql
    image: mysql:8.0
    ports:
      - 3306:3306
    tty: true
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_ROOT_HOST=%
      - TZ=Asia/Shanghai # 设置时区
    restart: always
    networks:
      nn:
        ipv4_address: 192.168.5.102
    volumes: 
      - /app/mysql:/app
      - /etc/localtime:/etc/localtime
      # - /app/mysql/data:/var/lib/mysql
    privileged: true

  
  nginx:
    container_name: nginx
    image: nginx
    ports: 
      - 80:80
      - 81:81
      - 82:82
      - 83:83
      - 443:443
      - 9001:9001
      - 9002:9002
      - 9003:9003
      - 9004:9004
      - 9005:9005
      - 9006:9006
      - 9007:9007
      - 9008:9008
      - 9009:9009
      - 9010:9010
      - 9011:9011
      - 9012:9012
    restart: always
    tty: true
    networks:
      nn:
        ipv4_address: 192.168.5.101
    volumes: 
      # - /app/nginx/default.conf:/etc/nginx/conf.d/default.conf
      # - /app/nginx/nginx.conf:/etc/nginx/nginx.conf
      - /app/nginx/app:/app
      - /etc/localtime:/etc/localtime

  python:
    container_name: python
    image: python:3.8.0
    restart: always
    tty: true
    command: bash
    working_dir: /app
    ports:
      - 5000:5000
    volumes:
      - /app/python:/app
      - /etc/localtime:/etc/localtime
    networks:
      nn:
        ipv4_address: 192.168.5.107
  
  tomcat:
    container_name: tomcat
    image: guyang201/tomcat:g1
    ports: 
      - 8081:8080
    restart: always
    tty: true
    environment:
      - TZ=Asia/Shanghai
    networks:
      nn:
        ipv4_address: 192.168.5.110
    volumes: 
      - /app/tomcat/app:/app
      # - /app/tomcat/webapps:/usr/local/tomcat/webapps
      - /etc/localtime:/etc/localtime

  mongo:  # MongoDB服务配置
    container_name: mongo
    image: mongo
    restart: always
    ports:
      - 27017:27017
    volumes:
      - /app/mongo/data:/data/db
    networks:
      nn:
        ipv4_address: 192.168.5.108

networks: ##定义网络组
  nn: ## 网络组名称
    driver: bridge ## 网络的模式
    ipam: ## 配置网络
      driver: default
      config:
        - subnet: 192.168.5.0/24 ## ip地址网络 这里宿主机一般会是该网段的 192.168.5.1,所以不要设置自网段为1
EOF
else
    echo "docker-compose.yml文件已存在，跳过生成步骤。"
fi
echo "docker-compose.yml文件生成完成"

echo "所有操作已完成。"

